var PATH_PREFIX =ctx + 'sys/oper/role/';
var ROLE_AUTH_PATH_PREFIX=ctx + 'sys/oper/roleAuth/';
var MENU_PATH_PREFIX =ctx + 'sys/menu/';

var WASP_ROLE = {
    roleAddView: function() {
        /** 新建 */
    	 var actionUrl = PATH_PREFIX+"roleAddView.do";
		 openDialog(actionUrl);
    },
    operRoleManageView: function() {
        /**角色权限管理页面 */
    	 var actionUrl = ROLE_AUTH_PATH_PREFIX+"operRoleManageView.do";
    	 openDialog(actionUrl, {"width": 1280, "left": Math.round((window.screen.width-1250)/2)});
    },
    roleUpdateView: function(roleId) {
	    /** 更新角色 */
		 var actionUrl = PATH_PREFIX+"roleUpdateView.do?roleId="+roleId;
		 openDialog(actionUrl);
	},
	formatViewHtml: function(text, options, rData) {
        var roleId = rData["roleId"];
        return '<a href="javascript(0);"  class="showunderline" data-toggle="modal" data-target="#modal-info"  onclick="WASP_ROLE.roleDetailView(\''+roleId+'\');" >' + text + '</a>';
    },
	roleDetailView: function(roleId){
		/**详情**/
		 var actionUrl = PATH_PREFIX+"roleDetailView.do?roleId="+roleId;
		 openDialog(actionUrl);
	},
	deleteRole: function(roleId){
		/**删除角色**/
		ctools.confirm({title : "删除角色", text: "数据删除后，不可修复"},function(isConfirm){
			var actionUrl = PATH_PREFIX+"deleteRole.do?roleId=" + roleId;
			if(isConfirm){
				$.ajax({
					type: 'POST',
					url: actionUrl,
					async: false,
					success: function(data){
						swal("删除成功!", "", "success");
						queryByCondtion(false);
					},
					error:function(xhr){
						swal("删除出错!", "", "error");
					}
				});
			}
			
		});
	},
	refreshMenuCache: function(){
		 var actionUrl = MENU_PATH_PREFIX+"refreshMenuCache.do";
		 openDialog(actionUrl);
	}
};


var $roleList=$("#roleList");
$(function(){
	$roleList.jqGrid({   
			url:PATH_PREFIX+'roleListPage.do',    
			caption:'角色维护列表<button type="button" class="btn btn-default btn-sm btn-small btn-fullscreen" title="" tabindex="-1"><i class="fa fa-arrows-alt icon-fullscreen"></i></button>',  
			datatype: "json",
			colNames:["角色ID","角色名称","创建人","创建时间", '备注', '操作'],        
			colModel:[  {name:'roleId',index:'ROLE_ID', hidden:true,key:true},
						{name:'roleName',index:'ROLE_NAME',width: 30, resizable:true,align:'left',formatter: WASP_ROLE.formatViewHtml,sortable:false},  
						{name:'createName',index:'CREATE_NAME', width: 20, resizable:true,align:'left', sortable:false},
						{name:'createTime',index:'CREATE_TMIE', width: 30, resizable:true,align:'left', sortable:false},
						{name:'remark',index:'REMARK', width: 40, resizable:true,align:'left',sortable:false},
						{name:'option',index:'option', width: 20, resizable:true,align:'left',sortable:false}
					  ],
			rowNum: 20,
	        rowList: [20, 30, 50],
	        rownumbers: true,
	        rownumWidth: 50,
	        prmNames: { search: "search", page: "pageNo", rows: "limit" },
	        height: 'auto',
	        width: false,
	        autowidth:true,
	        editurl: '',
	        viewrecords: true,
	        cellEdit: false,
	        shrinkToFit: true,
	        grouping: false,
	        jsonReader: {
	            root: "items", //结果集
	            records: "total", //总记录数 
	            total: "pageCount", //总页数
	            page: "pageNo", //当前页 
	            repeatitems: false // (4) 
	        },  
			multiselect:false,
			pager: "#rolePage", 							
			viewrecords: true,
			hidegrid: false, 
			gridComplete: function(){
				var ids = $roleList.jqGrid('getDataIDs');
				for(var i=0;i < ids.length;i++){
					var role_ID = ids[i];
				 
					var rowData = $roleList.jqGrid('getRowData', role_ID); 
					var ve = '<a href="javascript:void(0);" class="btn btn-link btn-jqgrid" title="详情" onclick="WASP_ROLE.roleDetailView(\''+rowData.roleId+'\');"  >详情</a>';
					var se = '<a id="lineEdit'+role_ID+'" href="javascript:void(0);" class="btn btn-link btn-jqgrid" title="修改" onclick="WASP_ROLE.roleUpdateView(\''+rowData.roleId+'\')">修改</a>';
					var  be = '<a  id="delete-' + role_ID + '" href="javascript:void(0);" class="btn btn-link btn-jqgrid" title="删除" onclick="WASP_ROLE.deleteRole(\''+rowData.roleId+'\');" >删除</a>';
					$roleList.jqGrid('setRowData',ids[i],{option:ve+se+be});
				}	
			}
		});
		$roleList.navGrid('#rolePage',{edit:false,add:false,del:false,search:false,refreshstate:'current'});
		$roleList.jqGrid('setFrozenColumns');
		jqGridResize($roleList);
	});

/**
 * 通过条件进行搜索
 */
function queryByCondtion(flag) {
    var name = $.trim($("#q-name").val());
    var postData = $roleList.jqGrid("getGridParam", "postData");
	$.extend(postData, {
		'sp[roleName]':name
	});
    if (flag) {
    	$roleList.trigger("reloadGrid", [{ page: 1 }]); //重新载入Grid表格
    } else {
    	$roleList.trigger("reloadGrid"); //重新载入Grid表格
    }
};
